package com.grape.dp.singleton.静态内部类;

/**
 * 描述:SingletonV4
 * @author: myx
 * @date: 2019/7/29 0029
 * Copyright © 2019-grape. All rights reserved.
 */
public class SingletonV4 {
    private SingletonV4() {
        System.out.println("构造函数被初始化...");
    }

    public static SingletonV4 getInstance() {
        return SingletonV5Utils.singletonV4;
    }

    // 在类里面嵌套的
    private static class SingletonV5Utils {
        private static final SingletonV4 singletonV4 = new SingletonV4();
    }

    /**
     * 内部类在调用的时候才会初始化singletonV5
     * static 静态 保证唯一
     * @param args
     */
    // 静态内部类特征：继承懒汉式和饿汉式优点、同时解决双重检验锁第一次加载慢的问题 读和写都不需要同步效率非常高...
    public static void main(String[] args) {
        System.out.println("项目启动成功...");
        SingletonV4 instance1 = SingletonV4.getInstance();
        SingletonV4 instance2 = SingletonV4.getInstance();
        System.out.println(instance1 == instance2);
    }
}
